<?php

namespace Qwadmin\Model;

use Think\Model;

class NovelModel extends Model {

    public $novel_db;
    public $prefix;

    function __construct() {
        
    }

    //获取推荐书籍列表
    public function get_recom($pram) {
        $prefix = C('DB_PREFIX');
        $page = intval($pram['page']) ?  $pram['page'] : 1;         //当前分页页码
        $pageSize = intval($pram['rows']) ? $pram['rows'] : 10;     //每页显示记录数
        $where = [
            'is_offline' => 1,
            'free' => 0
        ];
        //按标题检索小说
        $keyword = htmlspecialchars(I("keyword") , ENT_NOQUOTES);
        $type = htmlspecialchars(I("type") , ENT_NOQUOTES);
        if($keyword != ''){
            $where['book_title'] = ['like' , '%' . $keyword . '%'];
        }
        $order = "book_index desc,{$prefix}bookinfo.id desc";
        
        if ($type == 'new') {//查询新上架书籍
            $order = "{$prefix}bookinfo.create_t desc , {$prefix}bookinfo.id desc";
        } else if ($type == 'up') {
            $order = "{$prefix}bookinfo.update_time desc , {$prefix}bookinfo.id desc";
        }
        
        $first = $pageSize * ($page - 1);
        $data['list']  = M("bookinfo")
                            ->join("left join {$prefix}booktype on {$prefix}booktype.id = {$prefix}bookinfo.tid")
                            ->field("{$prefix}bookinfo.id as bookid ,book_title,book_img ,typename,book_status,book_index")       //我只要标题，分类名称，图片就够了
							->where($where)
                            ->order($order)
                            ->limit($first, $pageSize)
                            ->select();
        $data["count"] = M("bookinfo")->where($where)->count('id');
        return $data;
    }

    /**
     *  获取小说信息
     *  @param int $bookid 小说编号
     *  @param string $field 字段筛选 
     */
    public function get_bookinfo($bookid , $field="*"){
        $bookinfo = M("bookinfo")->field($field)
                    ->where(['id' => $bookid])->find();
        return $bookinfo;
    }
    
    /**
     * 获取章节列表
     * @param int $bookid 小说ID
     * @param int $rows  显示章节数量
     */
    public function get_chapter($bookid ,$rows = 20){
        $where['tid'] = $bookid;
        $where['is_offline'] = 1 ;
        
        $chapter = M("bookchapter")->field("id,chapter_name,sort")
                        ->where($where)
                        ->order('sort asc')
                        ->limit($rows)
                        ->select();
        return $chapter;
    }
    
    //获取书籍标题
    public function get_booktitle() {
        $book = htmlspecialchars(I("book", ''), ENT_NOQUOTES);
        if (empty($book)) {//查询书籍为空时，返回err
            return 'err';
        }
        $book_ret = $this->novel_db->table($this->prefix . "bookinfo")
                ->join($this->prefix . 'booktype on ' . $this->prefix . 'booktype.id = ' . $this->prefix . 'bookinfo.tid')
                ->where([$this->prefix . "bookinfo.id" => ['in', $book]])
                ->field($this->prefix . "bookinfo.id,book_title,typename")
                ->select();
        $data = [];
        foreach ($book_ret as $value) {
            $data[$value['id']] = '【' . $value['typename'] . '】' . $value['book_title'];
        }
        if (count($data) == 0) {
            echo 'err';
            die();
        }
        return json_encode($data);
    }

    //获取用户列表
    public function get_userlist() {
        $ret = ['errno' => 0, 'errmsg' => '', 'data' => ''];
        //try{
        $refid = empty(I("refid")) ? 0 : I("refid");
        $page = empty(I("page")) ? 1 : I("page");
        $pageSize = empty(I("rows")) ? 20 : I("rows");

        //搜索条件
        $s_uid = empty(I("s_uid")) ? 0 : trim(I("s_uid")); //搜索用户ID 为0不搜索
        $s_name = empty(I("s_name")) ? "" : trim(I("s_name")); //搜索用户名称 为空不搜索
        $s_subscribe = empty(I("s_subscribe")) ? 0 : trim(I("s_subscribe")); //搜索用户是否关注 为0搜索全部 1关注 2 未关注
        $s_recharge = empty(I("s_recharge")) ? 0 : trim(I("s_recharge")); //搜索用户是否充值 为0搜索全部 1充值 2 未充值
        //if(empty($refid) || !is_numeric($refid) || intval($refid).''!= $refid){
        //	throw new \Exception('推广员ID错误');
        //}
        if (!is_numeric($page) || intval($page) . '' != $page) {
            $page = 1;
        }
        if (!is_numeric($pageSize) || intval($pageSize) . '' != $pageSize) {
            $pageSize = 20;
        }
        $refid = intval($refid);
        $where = [
            'refid' => array('neq', 0),
            'source' => 'cps'
        ];
        if (!empty($refid)) {
            $where['refid'] = $refid;
        }
        if (!empty($s_uid) && is_numeric($s_uid) && intval($s_uid) . '' == $s_uid) {
            $where['uid'] = intval($s_uid);
        }
        if (!empty($s_name)) {
            $where['username'] = array('like', '%' . $s_name . '%');
        }
        if (!empty($s_subscribe) && is_numeric($s_subscribe) && intval($s_subscribe) . '' == $s_subscribe) {
            $where['subscribe'] = (intval($s_subscribe) == 1 ? 1 : 0);
        }
        if (!empty($s_recharge) && is_numeric($s_recharge) && intval($s_recharge) . '' == $s_recharge) {
            $where['integral'] = array(intval($s_recharge) == 1 ? 'gt' : 'elt', 0);
        }
        $first = $pageSize * ($page - 1);
        $prefix = C('DB_PREFIX');
        $ret['data']["list"] = M("user")
                ->field("uid,username,head_portrait,book_bean,integral,time,subscribe,refid")
                ->where($where)
                ->limit($first, $pageSize)
                ->order('uid desc')
                ->select();
        $ret['data']["count"] = M("user")->where($where)->count('uid');
        //}catch(\Exception $e){
        //	$ret['errno']=1;
        //	$ret['errmsg']=$e->getMessage();
        //}
        echo json_encode($ret);
        die;
    }

    /**
     * 统计充值金额
     * */
    public function order_census() {

        $this->novel_db->table($this->prefix . "userrecharge")
                ->join($this->prefix . 'booktype on ' . $this->prefix . 'booktype.id = ' . $this->prefix . 'bookinfo.tid')
                ->where([$this->prefix . "bookinfo.id" => ['in', $book]])
                ->field($this->prefix . "bookinfo.id,book_title,typename")
                ->select();
    }

}
